package top.rainbowecho.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.rainbowecho.article.mapper.CategoryMapper;
import top.rainbowecho.article.service.ICategoryService;
import top.rainbowecho.article.util.RedisProxy;
import top.rainbowecho.common.util.CacheKey;
import top.rainbowecho.model.percona.Category;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文章的分类 服务实现类
 * </p>
 *
 * @author 杨健
 * @since 2020-07-25
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Resource
    private RedisProxy redisProxy;

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    public void saveCategory(Category category) {
        this.categoryMapper.insert(category);
        this.redisProxy.evict(CacheKey.CATEGARY);
    }

    @Override
    @Transactional
    public List<Category> findAllCategory() {
        return this.redisProxy.getFromCache(CacheKey.CATEGARY, () -> this.categoryMapper.selectList(null));
    }
}
